﻿using FangDuShi_Admin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FangDuShi_Admin.Controllers
{
          [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PasswordController : AdminBaseController
    {

        BLL.Login loginBll = new BLL.Login();
        //
        // GET: /Password/
        public ActionResult Index()
        {

            string FLogID = OperateSession.get("LoginID");

            Model.PrvUser mdl = loginBll.GetModel(FLogID);
            return View(mdl);
        }

        [HttpPost]
        public string ExitstByPassword1(string newPassword1)
        {
            try
            {
                string FLogID = OperateSession.get("LoginID");

                Model.PrvUser mdl = loginBll.GetModelByNameAndPassword(FLogID, newPassword1);
                if (mdl == null)
                {
                    return "true";
                }
                else
                {

                    return "false";
                }
            }
            catch (System.Exception ex)
            {
                return "false";
            }
        }

        [HttpPost]
        public string ExitstByPasswordold(string oldPassword)
        {
            try
            {
                string FLogID = OperateSession.get("LoginID");

                Model.PrvUser mdl = loginBll.GetModelByNameAndPassword(FLogID, oldPassword);
                if (mdl == null)
                {
                    return "false";
                }
                else
                {
                    return "true";
                    ;
                }
            }
            catch (System.Exception ex)
            {
                return "false";
            }
        }
        [HttpPost]
        public string ExitstByPassword(string newPassword)
        {
            try
            {
                string FLogID = OperateSession.get("LoginID");

                Model.PrvUser mdl = loginBll.GetModelByNameAndPassword(FLogID, newPassword);
                if (mdl == null)
                {
                    return "true";
                }
                else
                {

                    return "false";
                }
            }
            catch (System.Exception ex)
            {
                return "false";
            }
        }
        [HttpPost]
        public string EditPassword()
        {
            string errMsg = "";
            try
            {
                string FLogID = OperateSession.get("LoginID");

                string oldPassword = Fetch.Post("oldPassword");

                if (string.IsNullOrEmpty(oldPassword))
                {
                    errMsg += "原始密码不能为空";
                    return errMsg;
                }
                if (ExitstByPasswordold(oldPassword).ToLower() != "true")
                {
                    errMsg += "原始密码错误";
                    return errMsg;
                }
                string newPassword = Fetch.Post("newPassword");
                string newPassword1 = Fetch.Post("newPassword1");
                if (newPassword != newPassword1)
                {

                    errMsg += "两次输入新密码不匹配";
                    return errMsg;
                }
                if (ExitstByPassword(newPassword).ToLower() == "false")
                {
                    errMsg += "新密码已存在请选择新的密码";
                    return errMsg;
                }

                if (string.IsNullOrEmpty(newPassword1))
                {
                    errMsg += "新密码不能为空";
                    return errMsg;
                }
                if (string.IsNullOrEmpty(newPassword))
                {
                    errMsg += "新密码不能为空";
                    return errMsg;
                }
                loginBll.EditPassword(FLogID, newPassword1);
                return "ok";
            }
            catch (System.Exception ex)
            {
                return errMsg + ex.Message;
            }

        }
    }
}